import 'package:async/async.dart';
import 'package:boss/models/store_area.dart';
import 'package:boss/provider/table_provider.dart';
import 'package:boss/ui/page/tableManagement/widget/table_management_area_card.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class TableManagementContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Loader<List<StoreArea>>(
      loadTask: () => _loadTask(context),
      builder: (context) {
        return Consumer<TableProvider>(
          builder: (context, tableProvider, _) {
            if (tableProvider.storeAreas.length == 0) return EmptyWidget();

            return ListView.builder(
              itemBuilder: (context, index) {
                var current = tableProvider.storeAreas[index];

                return TableManagementAreaCard(
                  data: current,
                );
              },
              itemCount: tableProvider.storeAreas.length,
            );
          },
        );
      },
    );
  }

  Future<Result<List<StoreArea>>> _loadTask(BuildContext context) async {
    var tableProvider = Provider.of<TableProvider>(
      context,
      listen: false,
    );
    return tableProvider.initData();
  }
}
